package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldProject;
import com.cleaningcloud.cld.domain.CldProjectEstimate;
import com.cleaningcloud.cld.domain.vo.CldProjectEstimateVo;
import com.cleaningcloud.cld.mapper.CldProjectEstimateMapper;
import com.cleaningcloud.cld.mapper.CldProjectMapper;
import com.cleaningcloud.cld.service.ICldProjectEstimateService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 项目估算Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-10
 */
@Service
public class CldProjectEstimateServiceImpl implements ICldProjectEstimateService {
    @Autowired
    private CldProjectEstimateMapper cldProjectEstimateMapper;

    @Autowired
    private CldProjectMapper cldProjectMapper;

    /**
     * 查询项目估算
     *
     * @param projectId 项目Id
     * @return 项目估算
     */
    @Override
    public CldProjectEstimateVo selectCldProjectEstimateByProjectId(Long projectId) {
        //查询数据
        List<CldProjectEstimate> cldProjectEstimateList = cldProjectEstimateMapper.selectCldProjectEstimateByProjectId(projectId);
        if (!cldProjectEstimateList.isEmpty()) {
            CldProjectEstimateVo cldProjectEstimateVo = new CldProjectEstimateVo();
            CldProject cldProject = cldProjectMapper.selectCldProjectByProjectId(projectId);
            if (cldProject != null) {
                cldProjectEstimateVo.setProjectId(projectId);
                cldProjectEstimateVo.setTaxation(cldProject.getTaxation());
                cldProjectEstimateVo.setYearFee(cldProject.getYearFee());
            }
            List<CldProjectEstimate> unitList = new ArrayList<>();
            List<CldProjectEstimate> unitList1 = new ArrayList<>();
            List<CldProjectEstimate> unitList2 = new ArrayList<>();
            List<CldProjectEstimate> unitList3 = new ArrayList<>();
            List<CldProjectEstimate> unitList4 = new ArrayList<>();
            // 计算总人数
            int sumNumber = cldProjectEstimateList.stream()
                    .filter(item -> 1 == item.getEstimateType())
                    .mapToInt(CldProjectEstimate::getBusinessNumber)
                    .sum();
            for (CldProjectEstimate projectEstimate : cldProjectEstimateList) {
                if (projectEstimate.getEstimateType() != null) {
                    if (projectEstimate.getEstimateType() == 1) {
                        projectEstimate.setBusinessUnitPriceTotal(new BigDecimal(projectEstimate.getBusinessNumber()).multiply(projectEstimate.getBusinessUnitPrice()));
                        cldProjectEstimateVo.setBusinessNumber(cldProjectEstimateVo.getBusinessNumber() + projectEstimate.getBusinessNumber());
                        unitList.add(projectEstimate);
                    }
                    if (projectEstimate.getEstimateType() == 2) {
                        unitList1.add(projectEstimate);
                    }
                    if (projectEstimate.getEstimateType() == 3) {
                        projectEstimate.setBusinessUnitPriceTotal(new BigDecimal(projectEstimate.getBusinessNumber()).multiply(projectEstimate.getBusinessUnitPrice()));
                        unitList2.add(projectEstimate);
                    }
                    if (projectEstimate.getEstimateType() == 4) {
                        if (projectEstimate.getCalcType() == 1) {
                            projectEstimate.setBusinessUnitPriceTotal(new BigDecimal(sumNumber).multiply(projectEstimate.getBusinessUnitPrice()));
                        } else {
                            projectEstimate.setBusinessUnitPriceTotal(projectEstimate.getBusinessUnitPrice());
                        }
                        unitList3.add(projectEstimate);
                    }
                }

            }
            cldProjectEstimateVo.setUnitList(unitList);
            cldProjectEstimateVo.setUnitList1(unitList1);
            cldProjectEstimateVo.setUnitList2(unitList2);
            cldProjectEstimateVo.setUnitList3(unitList3);
            cldProjectEstimateVo.setUnitList4(unitList4);
            cldProjectEstimateVo.setEstimateList(cldProjectEstimateList);
            return cldProjectEstimateVo;
        }
        return null;
    }

    /**
     * 查询项目估算列表
     *
     * @param cldProjectEstimate 项目估算
     * @return 项目估算
     */
    @Override
    public List<CldProjectEstimate> selectCldProjectEstimateList(CldProjectEstimate cldProjectEstimate) {
        return cldProjectEstimateMapper.selectCldProjectEstimateList(cldProjectEstimate);
    }

    /**
     * 新增项目估算
     *
     * @param cldProjectEstimateVo 项目估算
     * @return 结果
     */
    @Override
    public int insertCldProjectEstimate(CldProjectEstimateVo cldProjectEstimateVo) {
        //删除原有的记录
        cldProjectEstimateMapper.deleteCldProjectEstimateByProjectId(cldProjectEstimateVo.getProjectId());
        List<CldProjectEstimate> cldProjectEstimateList = new ArrayList<>();
        Date date = DateUtils.getNowDate();
        for (CldProjectEstimate cldProjectEstimate : cldProjectEstimateVo.getEstimateList()) {
            cldProjectEstimate.setCreateTime(date);
            cldProjectEstimate.setCreateTime(date);
            cldProjectEstimate.setUpdateTime(cldProjectEstimate.getCreateTime());
            cldProjectEstimate.setUpdateBy(SecurityUtils.getUsername());
            cldProjectEstimate.setCreateBy(SecurityUtils.getUsername());
            cldProjectEstimate.setProjectId(cldProjectEstimateVo.getProjectId());
            cldProjectEstimateList.add(cldProjectEstimate);
        }
        //修改项目的值
        CldProject cldProject = cldProjectMapper.selectCldProjectByProjectId(cldProjectEstimateVo.getProjectId());
        if (cldProject != null) {
            cldProject.setYearFee(cldProjectEstimateVo.getYearFee());
            cldProject.setTaxation(cldProjectEstimateVo.getTaxation());
            cldProject.setUpdateTime(date);
            cldProject.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
            cldProject.setMonthlyProfit(cldProjectEstimateVo.getProfitMonth());
            cldProject.setAnnualProfit(cldProjectEstimateVo.getProfitYear());
            cldProjectMapper.updateCldProject(cldProject);
        }
        return cldProjectEstimateMapper.batchInsert(cldProjectEstimateList);
    }

    /**
     * 修改项目估算
     *
     * @param cldProjectEstimate 项目估算
     * @return 结果
     */
    @Override
    public int updateCldProjectEstimate(CldProjectEstimate cldProjectEstimate) {
        return cldProjectEstimateMapper.updateCldProjectEstimate(cldProjectEstimate);
    }

    /**
     * 批量删除项目估算
     *
     * @param projectEstimateIds 需要删除的项目估算主键
     * @return 结果
     */
    @Override
    public int deleteCldProjectEstimateByProjectEstimateIds(Long[] projectEstimateIds) {
        return cldProjectEstimateMapper.deleteCldProjectEstimateByProjectEstimateIds(projectEstimateIds);
    }

    /**
     * 删除项目估算信息
     *
     * @param projectEstimateId 项目估算主键
     * @return 结果
     */
    @Override
    public int deleteCldProjectEstimateByProjectEstimateId(Long projectEstimateId) {
        return cldProjectEstimateMapper.deleteCldProjectEstimateByProjectEstimateId(projectEstimateId);
    }
}
